package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.ModuleEntity;
import com.sir.tao.carhire.service.ModuleService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tao
 * @data 2021/12/6 10:48
 * @description: 管理员登录接口
 */
@RestController
@Api(tags = "汽车信号接口")
@RequestMapping("/module")
public class ModuleController {



    @Resource
    private ModuleService moduleService;

    /**
     * 新增一个型号
     * @param moduleEntity 型号实体
     * @return 新增一个型号
     */
    @PostMapping("/add")
    @ApiOperation("新增一个型号")
    @ApiImplicitParam(name = "新增一个型号", value = "新增一个型号")
    public CommonResult<?> addCar(@RequestBody ModuleEntity moduleEntity){
        boolean i = moduleService.save(moduleEntity);
        if(i){
            return CommonResult.success();
        }else {
            return CommonResult.fail("新增失败");
        }
    }

    /**
     * 获取所有型号
     * @return 获取所有型号
     */
    @GetMapping("/getAll")
    @ApiOperation("获取所有型号")
    @ApiImplicitParam(name = "获取所有型号", value = "获取所有型号")
    public CommonResult<?> getAllModule(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                        String search){
        List<ModuleEntity> list = moduleService.list(new LambdaQueryWrapper<ModuleEntity>()
                .like(!StringUtils.isEmpty(search),ModuleEntity::getCompany,search)
                .or()
                .like(!StringUtils.isEmpty(search),ModuleEntity::getModuleNumber,search)
        );
        PageUtil page = PageUtil.doPage(pageNo,pageSize, list);
        return CommonResult.success(page);
    }


    /**
     * 更新一个型号
     * @param moduleEntity 型号实体
     * @return 更新一个型号
     */
    @PutMapping("/update")
    @ApiOperation("更新一个型号")
    @ApiImplicitParam(name = "更新一个型号", value = "更新一个型号")
    public CommonResult<?> update(@RequestBody ModuleEntity moduleEntity){
        boolean i = moduleService.updateById(moduleEntity);
        if(i){
            return CommonResult.success();
        }else {
            return CommonResult.fail("更新失败");
        }
    }

}
